package service.configuration.xstream;

import java.util.HashMap;
import java.util.Map;

import service.configuration.FieldType;
import service.configuration.IFieldConfig;
import service.configuration.IFieldTypeConfig;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.basic.BooleanConverter;

@XStreamAlias("field")
public class FieldConfig implements IFieldConfig {
	
	private String column;
	private String name;
	private FieldType type;
	private FieldStringConfig string;
	private FieldTextConfig text;
	private FieldDateConfig date;
	private FieldNumberConfig number;
	private FieldEnumConfig enumeration;
	private IFieldTypeConfig typeConfig;
	private String label;
	private String info;
	@XStreamConverter(value=BooleanConverter.class, booleans={false}, strings={"true", "false"})
	private boolean hidden;
	@XStreamConverter(value=BooleanConverter.class, booleans={false}, strings={"true", "false"})
	private boolean readOnly;
	@XStreamConverter(value=BooleanConverter.class, booleans={false}, strings={"true", "false"})
	private boolean optional;
	@XStreamConverter(value=BooleanConverter.class, booleans={false}, strings={"true", "false"})
	private boolean inShortDisplay;
	
	private static final Map<FieldType, String> hibernateTypes;
	
	static {
		hibernateTypes = new HashMap<FieldType, String>();
		hibernateTypes.put(FieldType.string, "string");
		hibernateTypes.put(FieldType.date, "string");
		hibernateTypes.put(FieldType.number, "string");
		hibernateTypes.put(FieldType.text, "text");
		hibernateTypes.put(FieldType.enumaration, "string");
	}
	
	public FieldConfig(FieldConfig config) {
		this.column = config.getColumn();
		this.name = config.getColumn();
		this.type = config.getType();
		this.string = config.getString();
		this.text = config.getText();
		this.date = config.getDate();
		this.number = config.getNumber();
		this.typeConfig = config.getTypeConfig();
		this.label = config.getColumn();
		this.info = config.getInfo();
		this.hidden = config.isHidden();
		this.readOnly = config.isReadOnly();
		this.optional = config.isOptional();
		this.inShortDisplay = config.isInShortDisplay();
	}
	
	public FieldConfig(String name, String column, FieldType type, String label, String info,
                       boolean hidden, boolean readOnly, boolean optional, 
                       boolean inShortDisplay) {
		
		this.column = column;
		this.type = type;
		this.label = label;
		this.info = info;
		this.hidden = hidden;
		this.readOnly = readOnly;
		this.optional = optional;
		this.inShortDisplay = inShortDisplay;
		this.name = name;
	}
	
	public String getHibernateType() {
		return hibernateTypes.get(type);
	}
	
	public String getColumn() {
		return column;
	}
	public void setColumn(String column) {
		this.column = column;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public boolean isHidden() {
		return hidden;
	}
	public void setHidden(boolean hidden) {
		this.hidden = hidden;
	}
	public boolean isReadOnly() {
		return readOnly;
	}
	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}
	public boolean isOptional() {
		return optional;
	}
	public void setOptional(boolean optional) {
		this.optional = optional;
	}
	public boolean isInShortDisplay() {
		return inShortDisplay;
	}
	public void setInShortDisplay(boolean inShortDisplay) {
		this.inShortDisplay = inShortDisplay;
	}
	public FieldType getType() {
		return type;
	}
	public void setType(FieldType type) {
		this.type = type;
	}
	public FieldStringConfig getString() {
		return string;
	}

	public void setString(FieldStringConfig string) {
		this.string = string;
	}

	public FieldTextConfig getText() {
		return text;
	}

	public void setText(FieldTextConfig text) {
		this.text = text;
	}

	public FieldDateConfig getDate() {
		return date;
	}

	public void setDate(FieldDateConfig date) {
		this.date = date;
	}

	public FieldNumberConfig getNumber() {
		return number;
	}

	public void setNumber(FieldNumberConfig number) {
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setTypeConfig(IFieldTypeConfig typeConfig) {
		this.typeConfig = typeConfig;
	}
	
	public IFieldTypeConfig getTypeConfig() {
		return typeConfig;
	}

	public FieldEnumConfig getEnumeration() {
		return enumeration;
	}

	public void setEnumeration(FieldEnumConfig enumeration) {
		this.enumeration = enumeration;
	}
	
}
